import optparse
import os.path
import sqlite3


# # 连接数据库
# conn = sqlite3.connect('sqlDB')
# # 创建数据库游标
# c = conn.cursor()
# sql = 'SELECT host,name,value FROM table'
# # 执行sql语句
# c.execute(sql)

# def printCookies(cookiesDB):
#     global host, name, value
#     try:
#         conn = sqlite3.connect('cookies.sqlite')
#         c = conn.cursor()
#         c.execute('SELECT host,name,value FROM moz_cookies')
#         print('\n[*] -- Found cookies--')
#         for row in c:
#             host = str(row[0])
#             name = str(row[1])
#             value = str(row[2])
#         if 'baidu' in host.lower():
#             print('[+] Host: ' + host + ',Cookie: ' + name + ', Value: ' + value)
#
#     except Exception as e:
#         if 'encrypted' in str(e):
#             print('\n[*] Error reading your cookies database.')
#             print('[*] Upgrade your Python-Sqlite3 Library')
#
#
# printCookies('cookies.sqlite')


def printHistory(placesDB):
    try:
        conn = sqlite3.connect('places.sqlite')
        c = conn.cursor()
        c.execute(
            "select url,datetime(visit_data/1000000,'unixepoch'"
            "from moz_places, moz_historyvisits where visit_count >0 and"
            "moz_places.id == moz_historyvisits.place_id;")
        print('\n[*] -- Found History --')
        for row in c:
            url = str(row[0])
            date = str(row[1])
            print('[+]' + date + '-Visited:' + url)
    except Exception as e:
        if 'encypted' in str(e):
            print('\n[*] Error reading your places database.')
            print('[*] Upgrade your Python-Sqlite3 Library')


printHistory(r'E:\pyCharm.test\places.sqlite')


# def main():
#     parser = optparse.OptionParser('<usage>')
#     parser.add_option('-p', dest='pathName', type='string')
#     help = 'specify firefox profile path'
#     (options, args) = parser.parse_args()
#     pathName = options.pathName
#     if pathName is None:
#         print(parser.usage)
#         exit(0)
#     if not os.path.isdir(pathName):
#         print('[!] Path Does Not Exist: ' + pathName)
#         exit(0)
#     # cookiesDB = os.path.join(pathName, 'cookies.sqlite')
#     # if os.path.isfile(cookiesDB):
#     #     printCookies(cookiesDB)
#     # else:
#     #     print('[!] CookiesDB Does Not Exist:' + cookiesDB)
#     '''
#     placesDB=os.path.join(pathName,'places.sqlite')
#     if os.path.isfile(placesDB):
#         printHistory(placesDB)
#         printGoogle(placesDB)
#     else:
#         print('[!] PlacesDB Does Not Exist:'+ placesDB)
#     '''
